package butines.game;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import butines.core.Image;
import butines.core.ImageSheet;
import butines.core.game.anim.Animation;
import butines.core.game.anim.AnimationManager;
import butines.core.graphics.ImageManager;
import butines.game.image.ColorPalleta;
import butines.game.image.ImageHelper;

public class UnitModel {

	private String name;
	private Image source;
	private int numAngles;
	private int tileHeight;
	private int tileWidth;
	
	private Map<ColorPalleta, ImageSheet> skins = new HashMap<ColorPalleta, ImageSheet>();
	private List<Animation> animations = new LinkedList<Animation>();
	
	public UnitModel(String name, String imagePath, int numAngles, int tileWidth, int tileHeight) {
		this.name = name;
		this.source = ImageManager.getInstance().loadImage(imagePath);
		this.numAngles = numAngles;
		this.tileWidth = tileWidth;
		this.tileHeight = tileHeight;
	}
	
	public ImageSheet getSkin(ColorPalleta palleta) {
		ImageSheet sheet = skins.get(palleta);
		if (sheet == null) {
			Image image = ImageHelper.getInstance().createImage(source, palleta, numAngles);
			sheet = new ImageSheet(image, tileWidth, tileHeight);
			skins.put(palleta, sheet);
		}
		return sheet;
	}
	
	public AnimationManager createAnimationManager(ColorPalleta palleta) {
		AnimationManager animationManager = new AnimationManager(getSkin(palleta), numAngles);
		for (Animation animation: getAnimations()) {
			animationManager.add(animation);
		}
		return animationManager;
	}
	
	
	public int getTileHeight() {
		return tileHeight;
	}

	public int getTileWidth() {
		return tileWidth;
	}
	
	public int getNumAngles() {
		return numAngles;
	}

	public String getName() {
		return name;
	}

	public void addAnimation(String name, float duration, int... frames) {
		Animation anim = new Animation(name, duration, frames);
		animations.add(anim);
	}
	
	public List<Animation> getAnimations() {
		return animations;
	}

}
